﻿using System;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace VupSlashLauncher.Common
{
    class IAnimation
    {

        public static async void Appear(FrameworkElement element, Point endpoint = new Point(), double duration = .2)
        {
            await Task.Delay(0);
            //将所选控件的Visibility属性改为Visible，这里要首先执行否则动画看不到
            ObjectAnimationUsingKeyFrames VisbilityAnimation = new ObjectAnimationUsingKeyFrames();
            DiscreteObjectKeyFrame kf = new DiscreteObjectKeyFrame(Visibility.Visible, TimeSpan.FromSeconds(0));
            VisbilityAnimation.KeyFrames.Add(kf);
            element.BeginAnimation(Border.VisibilityProperty, VisbilityAnimation);

            //创建新的缩放动画
            TranslateTransform TT = new TranslateTransform();
            element.RenderTransform = TT;
            //创建缩放动画函数，可以自己修改
            EasingFunctionBase easeFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut };

            DoubleAnimation XAnimation = new DoubleAnimation(endpoint.X, 0, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            element.RenderTransform.BeginAnimation(TranslateTransform.XProperty, XAnimation);

            DoubleAnimation YAnimation = new DoubleAnimation(endpoint.Y, 0, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            element.RenderTransform.BeginAnimation(TranslateTransform.YProperty, YAnimation);

            //将所选控件的可见度按动画函数方式显现
            DoubleAnimation OpacityAnimation = new DoubleAnimation(0, 1, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            element.BeginAnimation(Border.OpacityProperty, OpacityAnimation);
        }
        public static async void Disappear(FrameworkElement element, Point endpoint = new Point(), double duration = .2)
        {
            await Task.Delay(0);
            //创建新的缩放动画
            TranslateTransform TT = new TranslateTransform();
            element.RenderTransform = TT;
            //创建缩放动画函数，可以自己修改
            EasingFunctionBase easeFunction = new PowerEase() { EasingMode = EasingMode.EaseInOut };

            DoubleAnimation XAnimation = new DoubleAnimation(endpoint.X, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            element.RenderTransform.BeginAnimation(TranslateTransform.XProperty, XAnimation);

            DoubleAnimation YAnimation = new DoubleAnimation(endpoint.Y, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            element.RenderTransform.BeginAnimation(TranslateTransform.YProperty, YAnimation);

            //将所选控件的可见度按动画函数方式消失
            DoubleAnimation OpacityAnimation = new DoubleAnimation(1, 0, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            element.BeginAnimation(Border.OpacityProperty, OpacityAnimation);

            //将所选控件的Visibility属性改为Collapsed，这样不占用空间
            ObjectAnimationUsingKeyFrames VisbilityAnimation = new ObjectAnimationUsingKeyFrames();
            DiscreteObjectKeyFrame kf = new DiscreteObjectKeyFrame(Visibility.Collapsed, TimeSpan.FromSeconds(duration));
            VisbilityAnimation.KeyFrames.Add(kf);
            element.BeginAnimation(Border.VisibilityProperty, VisbilityAnimation);
        }
        public static async void MoveTo(FrameworkElement element, Point endpoint = new Point(), double duration = .2)
        {
            await Task.Delay(0);
            TranslateTransform TT = new TranslateTransform();
            element.RenderTransform = TT;
            EasingFunctionBase easeFunction = new BackEase() { EasingMode = EasingMode.EaseOut, Amplitude = .4 };
            DoubleAnimation XAnimation = new DoubleAnimation(endpoint.X, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            DoubleAnimation YAnimation = new DoubleAnimation(endpoint.Y, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            element.RenderTransform.BeginAnimation(TranslateTransform.XProperty, XAnimation);
            element.RenderTransform.BeginAnimation(TranslateTransform.YProperty, YAnimation);
        }
        public static async void MoveIn(FrameworkElement element, Point endpoint = new Point(), double duration = .2)
        {
            await Task.Delay(0);
            TranslateTransform TT = new TranslateTransform();
            element.RenderTransform = TT;
            EasingFunctionBase easeFunction = new BackEase() { EasingMode = EasingMode.EaseOut, Amplitude = .4 };
            DoubleAnimation XAnimation = new DoubleAnimation(endpoint.X, 0, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            DoubleAnimation YAnimation = new DoubleAnimation(endpoint.Y, 0, new Duration(TimeSpan.FromSeconds(duration))) { EasingFunction = easeFunction };
            element.RenderTransform.BeginAnimation(TranslateTransform.XProperty, XAnimation);
            element.RenderTransform.BeginAnimation(TranslateTransform.YProperty, YAnimation);
        }

    }
}
